import { MtEntity } from '@/api/manager/mt'
import { MixCometd } from './blocks/mix/translator'
import { useMccStore } from './store'
import {
	mcc_audience,
	mcc_audience_del,
	mcc_conf_detail,
	mcc_dcs_conf_status,
	mcc_dcs_mt,
	mcc_meeting_simpleInfo,
	mcc_mt,
	mcc_mt_del,
	mcc_smcu_add,
	mcc_smcu_remove,
	mcc_sub_smcu_mt,
	mcc_sub_smcu_mt_del,
} from './store.comted'
import { MeetingEntity } from '@/api/manager/meeting'
import { useMixStore } from './blocks/mix/store'
import { useSelectStore } from './blocks/select/store'
import { SelectInfoEntity } from '@/api/manager/select/interface'
import { PollCometd } from './blocks/poll/translator'
import { usePollStore } from './blocks/poll/store'
import { useVipStore } from './blocks/vip/store'
import { ApplyMsgItem, CometdMessage } from '@/api/manager/helper/interface'
import { useMsgStore } from './blocks/helper/store'
import { StartMonitorCometd, StopMonitorCometd } from './blocks/monitor/interface'
import { CameraPosInfoData, IMonitorFailData } from './blocks/cameraRc/interface'
import { useMonitorStore } from './blocks/monitor/store'
import { useCameraRcStore } from './blocks/cameraRc/store'
import { IVideoSourceListItem } from '@/api/manager/cameraRc/interface'
import { FetchPlayVideoInfo } from '@/api/manager/playVideo/interface'
import { usePlayVideoStore } from './blocks/playVideo/store'
import { ReSetNtfDataProps, useMccMtInfoStore } from './blocks/mccMtInfo/store'
import { useTableLiveStore } from './blocks/main/table/live/store'
import { LiveEntity } from '@/api/manager/live/interface'
import { useAIStore } from './blocks/ai/store'
import { ErrorCometd, TvWallBatchpollCometd, TvWallCometd } from './blocks/tvwall/interface'
import { useTvWallStore } from './blocks/tvwall/store'
import { DcsEntity } from '@/api/manager/dcs/interface'
import { useVmpStore } from './blocks/vmp/store'
import { VmpEntity } from '@/api/manager/vmp/interface'
import { useMtVmpStore } from './blocks/mtVmp/store'
import { useManagerStore } from './blocks/main/manager/store'
import { useAutoVmpStore } from '@/components/AutoVmpDialog/store'
import { useLiveVideoStore } from './blocks/liveVideo/store'
import { useMccMtVideoStore } from './blocks/mccMtVideo/store'
import { VideoEntity } from '@/api/manager/liveVideo/interface'
import { IMccMeetingSimpleInfo } from './blocks/main/manager/translator'
import { useTableAllStore } from './blocks/main/table/all/store'
import { getItemInTree } from './blocks/main/table/all/utils'
import { FetchGetAllVideoReturnProps } from '@/api/manager/mccMtVideo/interface'
import { useSettingMeetingStore } from './blocks/meetingInfoSet/store'
import { IGetMeetingInfo } from '@/api/manager/main/interface'
type IData = {
	mcc_mt: { mts: MtEntity[] }
	mcc_sub_smcu_mt: { cascade: MeetingEntity; mts: MtEntity[] }
	mcc_audience: { mts: MtEntity[] }
	mcc_smcu_add: { cascade: MeetingEntity }
	mcc_smcu_remove: { cascade: { id: string } }
	mcc_mt_del: { mt: { interactive_mt: 0 | 1; mt_id: string; mt_key: string } }
	mcc_sub_smcu_mt_del: { cascade: MeetingEntity; mts: { mt_key: string }[] }
	mcc_audience_del: { mt: { mt_id: string; mt_key: string } }
	mcc_meeting_mix: MixCometd
	mcc_meeting_selectview: SelectInfoEntity
	mcc_meeting_poll: PollCometd
	mcc_meeting_uppoll: PollCometd
	mcc_meeting_viplist: { vips: MtEntity[] }
	mcc_meeting_sysmsg: CometdMessage
	mcc_monitor_start: StartMonitorCometd
	mcc_monitor_stop: StopMonitorCometd
	mcc_monitor_fail: IMonitorFailData
	mcc_mt_camera_pos_info: CameraPosInfoData
	mcc_mt_video_src_info: { vidsrc_alias_info: IVideoSourceListItem[] }
	mcc_mt_cur_video_src_info: { mt_id: string; cur_video_src_idx: string }
	mcc_message_play_status: FetchPlayVideoInfo
	mcc_message_recordrec_status: FetchGetAllVideoReturnProps
	mcc_page_refresh: null
	mcc_message_popup: CometdMessage
	mcc_meeting_mt_apply: { mt_applys: ApplyMsgItem[] }
	mcc_message_mt_detail_info: ReSetNtfDataProps
	live_conf: { live: 0 | 1 }
	live_conf_mt_add: LiveEntity
	live_conf_mt_del: LiveEntity
	mcc_mt_kis_devices: { devices: { mt_key: string; mt_id: string }[] }
	mcc_meeting_voiceInspire: { voice_activity_detection: 0 | 1; vacinterval?: number }
	mcc_meeting_tvwall: TvWallCometd
	mcc_meeting_tvwall_batchpoll: TvWallBatchpollCometd
	mcc_hdu_fail_msg: ErrorCometd
	mcc_meeting_speakerqueue: { speak_queue: MtEntity[] }
	mcc_dcs_mt: { mts: DcsEntity[] }
	mcc_dcs_conf_status: { mode: 0 | 1 | 2 }
	mcc_meeting_vmp: VmpEntity
	mcc_meeting_mtvmp: VmpEntity & { vmp_mt: { mt_id: string } }
	mcc_conf_release: { reason: string }
	mcc_conf_password: { password: string }
	mcc_conf_detail: IGetMeetingInfo
	mcc_meeting_simpleinfo: IMccMeetingSimpleInfo
	mcc_meeting_forcebrd: { force_brd: 0 | 1 }
}
export type IMessage = {
	[key in keyof IData]: {
		e164: string
		message_type: string
		mcc_message_type: key
		data?: IData[key]
		meeting_moid: string
	}
}[keyof IData]
/** 消息预处理 */
export function preHandle(message: IMessage) {
	if (message.mcc_message_type === 'mcc_meeting_mix') {
		const data = message.data
		useMccStore.setState({ mixMode: data ? (data.state === 0 ? 0 : data.mode) : 0 })
	}
}
/** 消息分发 */
export function distribute(message: IMessage) {
	switch (message.mcc_message_type) {
		/*删除终端*/
		case 'mcc_mt_del': {
			let data = message.data
			let mtKey = data?.mt.mt_key
			if (mtKey) mcc_mt_del(mtKey)
			break
		}
		/*删除观众终端*/
		case 'mcc_audience_del': {
			const data = message.data
			let mtKey = data?.mt.mt_key
			if (mtKey) mcc_audience_del(mtKey)
			break
		}
		/*删除观众终端*/
		case 'mcc_sub_smcu_mt_del': {
			const data = message.data
			let mts = data?.mts
			if (mts && mts.length > 0) mcc_sub_smcu_mt_del(mts.map((x) => x.mt_key))
			break
		}
		/*终端列表*/
		case 'mcc_mt': {
			const data = message.data
			let mts = data?.mts || []
			if (mts.length > 0) {
				mcc_mt(mts)
			}
			break
		}
		/*下级终端列表*/
		case 'mcc_sub_smcu_mt': {
			const data = message.data
			if (data) {
				mcc_sub_smcu_mt(data)
			}
			break
		}
		/*观众终端*/
		case 'mcc_audience': {
			const data = message.data
			let mts = data?.mts || []
			if (mts.length > 0) {
				mcc_audience(mts)
			}
			break
		}
		/*级联添加会议节点*/
		case 'mcc_smcu_add': {
			const data = message.data
			let meeting = data?.cascade
			if (meeting) {
				mcc_smcu_add(meeting)
			}
			break
		}
		/*删除下级会议的终端  一个*/
		case 'mcc_smcu_remove': {
			const data = message.data
			let id = data?.cascade?.id
			if (id) {
				mcc_smcu_remove(id)
			}
			break
		}
		/*混音*/
		case 'mcc_meeting_mix': {
			const data = message.data
			if (data) useMixStore.getState().msgHandle(data)
			break
		}
		/*选看*/
		case 'mcc_meeting_selectview': {
			const data = message.data
			if (data) useSelectStore.getState().msgHandle(data)
			break
		}
		/*轮询*/
		case 'mcc_meeting_poll': {
			const data = message.data
			if (data) usePollStore.getState().mcc_meeting_poll(data)
			break
		}
		/*上传轮询*/
		case 'mcc_meeting_uppoll': {
			const data = message.data
			if (data) usePollStore.getState().mcc_meeting_uppoll(data)
			break
		}
		/*vip*/
		case 'mcc_meeting_viplist': {
			const data = message.data
			let vips = data?.vips || []
			useVipStore.getState().ws.mcc_meeting_viplist(vips)
			break
		}
		/*系统消息*/
		case 'mcc_meeting_sysmsg': {
			const data = message.data
			if (data) useMsgStore.getState().msgHandle(data)
			break
		}
		/*异常信息弹窗提示*/
		case 'mcc_message_popup': {
			const data = message.data
			if (data) useMsgStore.getState().msgPopup(data)
			break
		}
		/*终端申请*/
		case 'mcc_meeting_mt_apply': {
			const data = message.data
			if (data) useMsgStore.getState().updateApplyMsgList(data.mt_applys)
			break
		}
		/*会议监控*/
		case 'mcc_monitor_start': {
			const data = message.data
			if (data) useMonitorStore.getState().startMonitor(data)
			break
		}
		/*会议监控*/
		case 'mcc_monitor_stop': {
			const data = message.data
			if (data) useMonitorStore.getState().stopMonitor(data)
			break
		}
		/*监控开启失败*/
		case 'mcc_monitor_fail': {
			const data = message.data
			if (data) useCameraRcStore.getState().notifyFail(data)
			break
		}
		/*摄像机远遥预设位*/
		case 'mcc_mt_camera_pos_info': {
			const data = message.data
			if (data) useCameraRcStore.getState().notifyParse(data)
			break
		}
		/*摄像机远遥视频源列表*/
		case 'mcc_mt_video_src_info': {
			const data = message.data?.vidsrc_alias_info
			if (data) useCameraRcStore.getState().renderVideoSourceList(data)
			break
		}
		/*摄像机远遥视频源列表*/
		case 'mcc_mt_cur_video_src_info': {
			const data = message.data
			if (data) {
				const mt_id = useCameraRcStore.getState().mt_id
				if (data.mt_id === mt_id) {
					useCameraRcStore.getState().update({ curSource: data.cur_video_src_idx })
				}
			}
			break
		}
		/*放像状态*/
		case 'mcc_message_play_status': {
			const data = message.data
			if (data) usePlayVideoStore.getState().changePlayStatus(data)
			break
		}
		/*刷新界面*/
		case 'mcc_page_refresh': {
			setTimeout(function () {
				//防止cometd连不上的问题
				let url = window.location.href
				window.location.href = url
			}, 1500)
			break
		}
		/*终端信息*/
		case 'mcc_message_mt_detail_info': {
			const data = message.data
			if (data) useMccMtInfoStore.getState().reSetNtf(data)
			break
		}
		/*直播*/
		case 'live_conf': {
			const data = message.data
			if (data) useTableLiveStore.getState().ws.live_conf(data.live || 0)
			break
		}
		/*增加直播观看方*/
		case 'live_conf_mt_add': {
			const data = message.data
			if (data) useTableLiveStore.getState().ws.live_conf_mt_add(data)
			break
		}
		/*删除直播观看方*/
		case 'live_conf_mt_del': {
			const data = message.data
			if (data) useTableLiveStore.getState().ws.live_conf_mt_del(data)
			break
		}
		/*kis*/
		case 'mcc_mt_kis_devices': {
			const data = message.data
			let mts = data?.devices || []
			useAIStore.getState().ws_mcc_mt_kis_devices(mts)
			break
		}
		/*电视墙*/
		case 'mcc_meeting_tvwall': {
			const data = message.data
			if (data) useTvWallStore.getState().msgHandle(data)
			break
		}
		/*电视墙批量轮询*/
		case 'mcc_meeting_tvwall_batchpoll': {
			const data = message.data
			if (data) useTvWallStore.getState().batchpollhandle(data)
			break
		}
		/*电视墙批量轮询*/
		case 'mcc_hdu_fail_msg': {
			const data = message.data
			if (data) useTvWallStore.getState().errorHandle(data)
			break
		}
		/*语音激励*/
		case 'mcc_meeting_voiceInspire': {
			const data = message.data
			if (data) {
				useMccStore.setState((v) => ({
					...v,
					mccInfo: {
						...v.mccInfo,
						voiceInspireState: data.voice_activity_detection,
						voiceInspireTime: data.vacinterval || 3,
					},
				}))
			}
			break
		}
		/*数据协作终端*/
		case 'mcc_dcs_mt': {
			const data = message.data
			if (data) mcc_dcs_mt(data.mts)
			break
		}
		/*数据协作状态*/
		case 'mcc_dcs_conf_status': {
			const data = message.data
			if (data) mcc_dcs_conf_status(data.mode ?? 0)
			break
		}
		/*画面合成*/
		case 'mcc_meeting_vmp': {
			const data = message.data
			if (data) {
				useMccStore.setState((v) => ({ ...v, mccInfo: { ...v.mccInfo, vmpState: data.state ?? 0 } }))
				useVmpStore.getState()._messageInfo(data.state ?? 0) /**弹出消息*/
				useVmpStore.getState().msgHandle(data)
			}
			break
		}
		/*管理方画面合成*/
		case 'mcc_meeting_mtvmp': {
			const data = message.data
			if (data) {
				if (data.vmp_mt.mt_id === useManagerStore.getState().chairman?.mtid) {
					useMccStore.setState((v) => ({
						...v,
						mccInfo: { ...v.mccInfo, chairmanMtVmpState: data?.state ?? 0 },
					}))
					useMtVmpStore.getState()._messageInfo(data.state ?? 0)
					useMtVmpStore.getState().msgHandle(data)
				} else {
					useAutoVmpStore.getState().msgHandle(data)
				}
			}
			break
		}
		/*结束会议*/
		case 'mcc_conf_release': {
			const data = message.data
			if (data) {
				useSettingMeetingStore.getState().endMeeting(data)
			}
			break
		}
		/** 会议密码 */
		case 'mcc_conf_password': {
			const data = message.data
			if (data) {
				useMccStore.setState((v) => ({
					...v,
					mccInfo: { ...v.mccInfo, password: data.password },
				}))
			}
			break
		}
		/**强制广播  */
		case 'mcc_meeting_forcebrd': {
			const data = message.data
			if (data) {
				useMccStore.setState((v) => ({
					...v,
					mccInfo: { ...v.mccInfo, force_brd: data.force_brd },
				}))
			}
			break
		}
		/*会议详情 */
		case 'mcc_conf_detail': {
			const data = message.data
			if (data) {
				mcc_conf_detail({ subject: data.name })
				useMccStore.setState((v) => ({
					...v,
					mccInfo: {
						...v.mccInfo,
						subject: data.name,
						password: data.password,
						meetingClosed: data.closed_conf,
						meetingSafe: data.safe_conf,
						watermark: data.watermark,
						doubleFlowLimit: data.dual_mode,
						allowAnonymousMt: data.anonymous_mt,
						startTime: data.start_time,
						endTime: data.end_time,
						mute: data.mute,
						silence: data.silence,
						duration: data.duration,
						force_brd: data.force_brd,
						force_mute: data.force_mute ?? 0,
					},
				}))
			}
			break
		}
		/**会议（终端）录像通知 */
		case 'mcc_message_recordrec_status': {
			const data = message.data
			if (data) {
				if (data.type === 2) {
					if (useMccStore.getState().curPage === 'mt_record') {
						//终端录像
						useMccMtVideoStore.getState().storeRecord(data)
						if (data.state === 0) {
							//停止录像
							let recordRecIng = useMccMtVideoStore
								.getState()
								.recordRecIng.find((item) => item.id === data.id)
							if (recordRecIng) {
								useMccMtVideoStore.getState().cometdRerenderVideo({ ...recordRecIng, state: 0 })
								return
							}
						}
						useMccMtVideoStore.getState().cometdRerenderVideo(data)
					}
					return
				}
				if (useMccStore.getState().curPage === 'video_live') {
					useLiveVideoStore.getState().ws.mcc_message_recordrec_status(data as VideoEntity)
				}
			}
			break
		}
		case 'mcc_meeting_simpleinfo': {
			let data = message.data
			if (data) {
				const tree = useTableAllStore.getState().tree
				if (data.speaker && data.speaker.mt_key) {
					if (data.speaker.mt_type === 7) {
						data.speaker.conf_level = 3
					} else if (data.speaker.mt_type === 8) {
						data.speaker.conf_level = 1
					} else {
						getItemInTree(tree, (item) => {
							if (!item.mcc.isMt && item.mcc.account === data?.speaker.conf_e164) {
								data.speaker.conf_level = item.mcc.confLevel
							}
						})
					}
				}
				if (data.chairman && data.chairman.mt_key) {
					if (data.chairman.mt_type === 7) {
						data.chairman.conf_level = 3
					} else if (data.chairman.mt_type === 8) {
						data.chairman.conf_level = 1
					} else {
						getItemInTree(tree, (item) => {
							if (!item.mcc.isMt && item.mcc.account === data?.chairman.conf_e164) {
								data.chairman.conf_level = item.mcc.confLevel
							}
						})
					}
				}
				if (data.force_brd && data.force_brd.mt_key) {
					if (data.force_brd.mt_type === 7) {
						data.force_brd.conf_level = 3
					} else if (data.force_brd.mt_type === 8) {
						data.force_brd.conf_level = 1
					} else {
						getItemInTree(tree, (item) => {
							if (!item.mcc.isMt && item.mcc.account === data?.force_brd.conf_e164) {
								data.force_brd.conf_level = item.mcc.confLevel
							}
						})
					}
				}
				mcc_meeting_simpleInfo(data)
			}
			break
		}
		default:
			break
	}
}
